{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "import PIL import image\n",
    "import paddlex as pdx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DATADIR='./data/garbage'\n",
    "file1='paper/paper1.jpg'\n",
    "file2='glass/glass1.jpg'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img1=image.open(os.path.join(DATADIR,file1))\n",
    "img1=np.array(img1)\n",
    "img2=image.open(os.path.join(DATADIR,file2))\n",
    "img2=np.array(img2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize(16,8))\n",
    "f=plt.subplot(121)\n",
    "plt.imshow(img1)\n",
    "f=plt.subplot(122)\n",
    "plt.imshow(img2)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "DATADIR='./data/garbage'\n",
    "datadir=os.listdir(DATADIR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "file_list=[]\n",
    "label_list=[]\n",
    "train_ratio=0.8\n",
    "l=0\n",
    "f1=open('./train.txt','a+')\n",
    "f2=open('./val.txt','a+')\n",
    "f3=open('./labels.txt','a+')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in datadir:\n",
    "    s=os.listdir(os.path.join(DATADIR,i))\n",
    "    l+=len(s)\n",
    "    train_num=int(len(s)*train_ratio)\n",
    "    val_num=int(len(s)-train_ratio)\n",
    "    for file in s:\n",
    "    if file[0]=='p'and file[1]=='a':\n",
    "        file_list.append(i+'/'+file)\n",
    "        label_list.append(0)\n",
    "    elif file[0]=='p'and file[1]=='l':\n",
    "        file_list.append(i+'/'+file)\n",
    "        label_list.append(1)\n",
    "    elif file[0]=='g':\n",
    "        file_list.append(i+'/'+file)\n",
    "        label_list.append(2)\n",
    "f1.writelines([str(file_list[j])+''+str(label_list[j])+'\\n'for j in range(train_num)])\n",
    "f2.writelines([str(file_list[k])+''+str(label_list[k])+'\\n'for k in range(val_num)])\n",
    "f2.writelines([i+'\\n'])\n",
    "file_list=[]\n",
    "label_list=[]\n",
    "f1.close()\n",
    "f2.close()\n",
    "f3.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "txt_list=['/train.txt','./val.txt']\n",
    "l1=0\n",
    "for filename in txt_list:\n",
    "    with open(filename,'r')as f:\n",
    "        l1+=len(f.readlines())\n",
    "if l1==l:\n",
    "    print(\"拆分后数据集与元数据集无误\")\n",
    "elif l1!=l:\n",
    "    print(\"拆分后数据集有误\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib\n",
    "matplotlib.use('Agg')\n",
    "import os\n",
    "os.environ['CUDA_VISIBLE_DEVICES']='0'\n",
    "import paddlex as pdx\n",
    "import imghdr\n",
    "\n",
    "import paddle.fluid as fluid\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from PIL import image\n",
    "from paddlex.cls import transforms\n",
    "train_transforms=transforms.Compose([transforms.RandomCrop(crop_size=224),\n",
    "    trainsforms.RandomHorizontalFlip(),\n",
    "    transforms.RandomDistort(brightness_range=0.9,brightness_prob=0.5,contrast_range=0.9,\n",
    "    contrast_prob=0.5,saturation_range=0.9,saturation_prob=0.5,hue_range=18,hue_prob=0.5),\n",
    "    transforms.Normalize()])\n",
    "val_transforms=transforms.Compose([transforms.ResizeByShort(short_size=256),\n",
    "                                  transforms.CenterCrop(crop_size=224),\n",
    "                                  transforms.Normalize()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import paddlex as pdx\n",
    "train_dataset=pdx.datasets.imageNet(\n",
    "data_dir='./data/garbage',\n",
    "file_list='./train.txt',\n",
    "lagbel_list='./labels.txt',\n",
    "transforms=train_transforms,\n",
    "shuffle=True)\n",
    "val_dataset=pdx.datasets.imageNet(\n",
    "data_dir='./data/garbage',\n",
    "file_list='./val.txt',\n",
    "lagbel_list='./labels.txt',\n",
    "transforms=train_transforms,\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
